package com.zhumo.sz.common.easyexcel;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jiaqi.zhu
 * @comment 封装easyExcel 常用读取操作
 * @time 2024-03-19
 */
@Slf4j
public class EasyExcelReader {
    public static <T> List<T> readAsList(Class<T> mapClass, String path) {
        ArrayList<T> collect = new ArrayList<>();
        EasyExcel.read(path, mapClass, new AnalysisEventListener<T>() {
            @Override
            public void invoke(T t, AnalysisContext analysisContext) {
                collect.add(t);
            }
            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                log.info("读取完成");
            }
        }).sheet().doRead();
        return collect;
    }
}
